
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'fun/emp/changeStatusList',
        datatype: "json",
        colModel: [
            { label: '员工ID', name: 'empId', index: "emp_id", width: 90, key: true},
            { label: '员工名', name: 'empName', width: 60 },
            { label: '身份证号', name: 'idCard',sortable: false, width: 100 },
            { label: '所属部门', name: 'deptName', sortable: false,width: 60 },
            { label: '减少原因', name: 'status', sortable: false,width: 90, formatter: function(value, options, row){
                    if(value === 0)
                        return '<span >在岗</span>'
                    else if(value === 1)
                        return '<span >死亡</span>'
                    else if(value === 2)
                        return '<span >离职</span>'
                    else if(value === 3)
                        return '<span >退休</span>'
                    else if(value === 4)
                        return '<span >调出</span>'
                }},
            { label: '操作人员', name: 'operator', sortable: false, width: 90 },
            { label: '操作时间', name: 'date', sortable: false, width: 90 },
            { label: '备注', name: 'remarks', sortable: false, width: 90 },
        ],
        viewrecords: true,
        height: 400,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
             $("#jqGrid").setGridParam().hideCol("empId").trigger("reloadGrid");
        },
    });
});

//部门结构树
var dept_ztree;
var dept_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        emp: {
            remarks:null,
            status:null
        },
        q:{
            deptId:null
        },
        showList: true,
        empId:null,
        emp:{},
        empAttachment:{},
        selectDept:'',
        works: [],
        rewards: [],
        pickerOptions: {
            disabledDate(time) {
                return time.getTime() > Date.now();
            }
        },
        fileList: [],
        dialogTableVisible: false,
        dialogFormVisible: false,
        dialogDeptVisible: false,
        dialog: false,
        formLabelWidth: '80px',
        timer: null,
        changeStatusEmpIds:null,
        queryByDate:[],
        status: [{value:'1',label:'死亡'},{value:'2',label:'离职'},{value:'3',label:'退休'},{value:'4',label:'调出'}],
        activeNames: ['1'],
        addEmp: {},
        changeEmpDept: [],
        changeEmpOffice: [],
        empStatusChange:[],
        statusChange: {},
        reverse: true,
        formData:[
            {index: 0,  isShow: false}, {index: 1,  isShow: false},
            {index: 2,  isShow: false}, {index: 3,  isShow: false},
            {index: 4,  isShow: false}, {index: 5,  isShow: false},
            {index: 6,  isShow: false}, {index: 7,  isShow: false},
            {index: 8,  isShow: false}, {index: 9,  isShow: false},
            {index: 10,  isShow: false}
        ],
    },

    methods: {
        query: function () {
            vm.reload();
        },
        empShow:function ()
        {
            vm.empId = getSelectedRow();
            if(vm.empId == null){
                return ;
            }
            vm.showList = false;
            vm.getInfo(vm.empId);
            /*this.getWork();
            this.getReward();*/
            vm.getAddEmp(vm.empId);
            vm.getChangeEmpDept(vm.empId);
            vm.getChangeEmpOffice(vm.empId);
            vm.getEmpStatusChange(vm.empId);
            vm.getStatusChange(vm.empId);
        },
        update: function (event) {
            var empId = getSelectedRow();
            if(empId == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(empId);
            vm.getDept();
        },
        exportExcel: function (event) {
            var empIds = getSelectedRows();
            if(empIds == null){
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "fun/emp/exportEmps",
                contentType: "application/json",
                data: JSON.stringify(empIds),
            });
            vm.$message('导出到G://empExcel成功');

        },
        removeEmp:function()
        {
            vm.changeStatusEmpIds =getEmpSelectedRows();
            if(vm.changeStatusEmpIds == null){
                return ;
            }
            vm.emp={
                status:null,
                remarks:null
            };
            vm.dialog = true;
        },
        getInfo: function(empId){
            vm.formData=[
                {index: 0,  isShow: false}, {index: 1,  isShow: false}, {index: 2,  isShow: false},
                {index: 3,  isShow: false}, {index: 4,  isShow: false}, {index: 5,  isShow: false},
                {index: 6,  isShow: false}, {index: 7,  isShow: false}, {index: 8,  isShow: false},
                {index: 9,  isShow: false}, {index: 10,  isShow: false}
            ],
                vm.empAttachment={};
            $.get(baseURL + "fun/emp/info/"+empId, function(r){
                vm.emp = r.emp.empEntity;
                vm.selectDept=r.emp.deptParent;
                switch (vm.selectDept) {
                    case 'jwd':
                        vm.formData[0].isShow=true;
                        vm.empAttachment= r.emp.jwdEmpEntity;
                        console.log(vm.formData[0].isShow);
                        break;
                    case 'kyd':
                        vm.formData[1].isShow=true;
                        vm.empAttachment= r.emp.kydEmpEntity;
                        break;
                }
                vm.getDept();
            });
        },
        getWork: function(empId){
            $.get(baseURL + "fun/emp/work/"+empId, function(r){
                vm.works = r.work;
            });
        },
        getReward: function(empId){
            $.get(baseURL + "fun/emp/reward/"+empId, function(r){
                vm.rewards = r.reward;
            });
        },
        getAddEmp:function(empId)
        {
            $.get(baseURL + "fun/emp/infoAddEmp/"+empId, function(r){
                vm.addEmp = r.addEmp;
            });
        },
        getChangeEmpDept:function(empId)
        {
            $.get(baseURL + "fun/emp/infoChangeEmpDept/"+empId, function(r){
                vm.changeEmpDept = r.changeEmpDept;
            });
        },
        getChangeEmpOffice:function(empId)
        {
            $.get(baseURL + "fun/emp/infoChangeEmpOffice/"+empId, function(r){
                vm.changeEmpOffice = r.changeEmpOffice;
            });
        },
        getStatusChange:function(empId)
        {
            $.get(baseURL + "fun/emp/infoStatusChange/"+empId, function(r){
                vm.statusChange = r.statusChange;
            });
        },
        getEmpStatusChange:function(empId)
        {
            $.get(baseURL + "fun/emp/infoEmpStatusChange/"+empId, function(r){
                vm.empStatusChange = r.empStatusChange;
            });
        },
        handleCloseDrawer:function(done) {
            vm.dialog=false;
        },
        cancelForm:function() {
            vm.dialog = false;
        },
        changeStatus:function() {
            if(vm.emp.status==null)
            {
                alert('减少原因不能为空');
                return;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "fun/emp/changeStatus",
                contentType: "application/json",
                data: JSON.stringify({'empIds':vm.changeStatusEmpIds,'status':vm.emp.status,'remarks':vm.emp.remarks}),
                success: function(r){
                    if(r.code == 0){
                        vm.dialog = false;
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });

                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        handlePreview:function(file) {
            console.log(file);
        },
        handleRemove:function(file, fileList) {
            console.log(file, fileList);
        },
        handleChange(val) {
            console.log(val);
        },
        fresh:function (event)
        {
            $("#jqGrid").trigger("reloadGrid");
            vm.dialogFormVisible=false;
        },
        freshDept:function (event)
        {
            $("#jqGrid").trigger("reloadGrid");
            vm.dialogDeptVisible=false;
        },
        getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/list", function(r){
                dept_ztree = $.fn.zTree.init($("#deptTree"), dept_setting , r);
                var node = dept_ztree.getNodeByParam("deptId", vm.emp.deptId);
                if(node != null){
                    dept_ztree.selectNode(node);
                    vm.emp.deptName = node.name;
                }
            })
        },
        reload: function (event) {
            vm.showList = true;
            if(vm.queryByDate==null)
            {
                vm.queryByDate=['',''];
            }
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'dateStart': vm.queryByDate[0],'dateEnd': vm.queryByDate[1],'deptId':vm.q.deptId},
                page:page
            }).trigger("reloadGrid");
        }
    }
});
window.addEventListener("message",function(event){
    vm.showList = true;
    if(vm.queryByDate==null)
    {
        vm.queryByDate=['',''];
    }
    vm.q.deptId=event.data;
    var page = $("#jqGrid").jqGrid('getGridParam','page');
    $("#jqGrid").jqGrid('setGridParam',{
        postData:{'deptId':event.data,'dateStart': vm.queryByDate[0],'dateEnd': vm.queryByDate[1]},
        page:page
    }).trigger("reloadGrid");
}, false);